from Widget import BionicText
from . import BaseView
from tkinter import LabelFrame, Scrollbar, Label
import pyperclip


class BionicView(LabelFrame, BaseView):
    def __init__(self, parent, delegetor):
        super().__init__(parent, text="bionic")
        BaseView.__init__(self, delegetor)

        self.text_plate = BionicText(self, font=('宋体', 12), undo=True)
        self.scrollbal = Scrollbar(self, orient='vertical')
        self.scrollbal.pack(side='right', fill='y')
        self.text_plate.pack(side='right', fill='both')
        self.scrollbal['command'] = self.text_plate.yview
        self.text_plate['yscrollcommand'] = self.scrollbal.set
        self.text_plate.pack(fill='both', expand=True)

        self.text_plate.bind("<Control-MouseWheel>", self.zoom)
        self.text_plate.bind("<Double-Button-3>", self.showing)

    def set_config(self, config: dict):
        super().set_config(config)
        self.text_plate['font'] = config['font']

    def get_basic_config(self):
        return {
            "font": ('宋体', 12),
            "exec": "notepad"
        }

    def file_extension(self):
        return ".bionic"

    def zoom(self, event):
        delta = event.delta
        font_name, font_size = self.text_plate['font'].split(" ")
        font_size = int(font_size)
        if delta > 0:
            font_size += 1
        else:
            font_size -= 1
        self.text_plate['font'] = (font_name, font_size)
        self.config['font'] = (font_name, font_size)

    def showing(self, event):
        text = pyperclip.paste()
        self.text_plate.clear()
        self.text_plate.show(text)

    def open(self, path):
        pass

    def close(self):
        pass

    def is_plugin(self):
        return True